Complete these steps 3 times with mean vector = c(0,0,0), and differenct covariance matrices each time. Select values for your covariance matrices. Each time use 3-dimensions, X, Y, and Z.

1st Time

Multivariate normal density

Create a grid of values going from -3, to 3 for each dimension. Use expand.grid() to get the grid of values.

Compute the density of each point.

Plot a scatterplot matrix showing the density of points.

library(mvtnorm)

v <- seq(
  from = -3,
  to = 3,
  length = 20
)
M_multivariatenormal <- expand.grid(
  x = v,
  y = v,
  z = v
)
v_mean <- c(0,0,0)
M_sigma <- matrix(
  data = c(
    2,-1,0,
    -1,2,1,
    0,1,2
  ),
  nrow = 3,
  byrow = TRUE
)

M_multivariatenormal$density <- mvtnorm::dmvnorm(
  x = M_multivariatenormal,
  mean = v_mean,
  sigma = M_sigma
)
M_multivariatenormal$density_bucket <- ggplot2::cut_number(
  x = M_multivariatenormal$density,
  n = 4
)
v_color <- rev(gray.colors(
  n = length(levels(M_multivariatenormal$density_bucket)),
  start = 0,
  end = 1
))
names(v_color) <- levels(M_multivariatenormal$density_bucket)
M_multivariatenormal$color <- v_color[as.character(M_multivariatenormal$density_bucket)]
pairs(
  x = M_multivariatenormal[,c("x","y","z")],
  col = M_multivariatenormal$color,
  pch = 19,
  main = "Scatterplot matrix of the multivariate normal density 1"
)

Multivariate normal random number generation.

Randomly generate at least 100 multivariate normal points.

Plot a 3-d scatterplot of your points.

M_rnvnorm <- mvtnorm::rmvnorm(
  n = 100,
  mean = v_mean,
  sigma = M_sigma
)
colnames(M_rnvnorm) <- c("x","y","z")
M_rnvnorm <- as.data.frame(
  x = M_rnvnorm
)
p <- plotly::plot_ly(
  x = M_rnvnorm$x,
  y = M_rnvnorm$y,
  z = M_rnvnorm$z,
  type="scatter3d",
  mode="markers"
)
p

Multivariate probabilities

Compute the probability that an observation is in this region:

mvtnorm::pmvnorm(
  lower = c(-1,-2,-2),
  upper = c(2,1,3),
  mean = v_mean,
  sigma =M_sigma
)
## [1] 0.4652876
## attr(,"error")
## [1] 0.000116656
## attr(,"msg")
## [1] "Normal Completion"

2nd Try

Multivariate normal density

Create a grid of values going from -3, to 3 for each dimension. Use expand.grid() to get the grid of values.

Compute the density of each point.

Plot a scatterplot matrix showing the density of points.

library(mvtnorm)

v <- seq(
  from = -3,
  to = 3,
  length = 20
)
M_multivariatenormal <- expand.grid(
  x = v,
  y = v,
  z = v
)
v_mean <- c(0,0,0)
M_sigma <- matrix(
  data = c(
    1,-1,0,
    -1,2,1,
    0,1,3
    ),
  nrow = 3,
  byrow = TRUE
)

M_multivariatenormal$density <- mvtnorm::dmvnorm(
  x = M_multivariatenormal,
  mean = v_mean,
  sigma = M_sigma
)
M_multivariatenormal$density_bucket <- ggplot2::cut_number(
  x = M_multivariatenormal$density,
  n = 4
)
v_color <- rev(gray.colors(
  n = length(levels(M_multivariatenormal$density_bucket)),
  start = 0,
  end = 1
))
names(v_color) <- levels(M_multivariatenormal$density_bucket)
M_multivariatenormal$color <- v_color[as.character(M_multivariatenormal$density_bucket)]
pairs(
  x = M_multivariatenormal[,c("x","y","z")],
  col = M_multivariatenormal$color,
  pch = 19,
  main = "Scatterplot matrix of the multivariate normal density 2 "
)

Multivariate normal random number generation.

Randomly generate at least 100 multivariate normal points.

Plot a 3-d scatterplot of your points.

M_rnvnorm <- mvtnorm::rmvnorm(
  n = 100,
  mean = v_mean,
  sigma = M_sigma
)
colnames(M_rnvnorm) <- c("x","y","z")
M_rnvnorm <- as.data.frame(
  x = M_rnvnorm
)
p <- plotly::plot_ly(
  x = M_rnvnorm$x,
  y = M_rnvnorm$y,
  z = M_rnvnorm$z,
  type="scatter3d",
  mode="markers"
)
p

Multivariate probabilities

Compute the probability that an observation is in this region:

mvtnorm::pmvnorm(
  lower = c(-1,-2,-2),
  upper = c(2,1,3),
  mean = v_mean,
  sigma =M_sigma
)
## [1] 0.5304862
## attr(,"error")
## [1] 0.0005668327
## attr(,"msg")
## [1] "Normal Completion"

3rd Try

Multivariate normal density

Create a grid of values going from -3, to 3 for each dimension. Use expand.grid() to get the grid of values.

Compute the density of each point.

Plot a scatterplot matrix showing the density of points.

library(mvtnorm)

v <- seq(
  from = -3,
  to = 3,
  length = 20
)
M_multivariatenormal <- expand.grid(
  x = v,
  y = v,
  z = v
)
v_mean <- c(0,0,0)
M_sigma <- matrix(
  data = c(
    6,-1,0,
    -1,5,1,
    0,1,4
  ),
  nrow = 3,
  byrow = TRUE
)

M_multivariatenormal$density <- mvtnorm::dmvnorm(
  x = M_multivariatenormal,
  mean = v_mean,
  sigma = M_sigma
)
M_multivariatenormal$density_bucket <- ggplot2::cut_number(
  x = M_multivariatenormal$density,
  n = 4
)
v_color <- rev(gray.colors(
  n = length(levels(M_multivariatenormal$density_bucket)),
  start = 0,
  end = 1
))
names(v_color) <- levels(M_multivariatenormal$density_bucket)
M_multivariatenormal$color <- v_color[as.character(M_multivariatenormal$density_bucket)]
pairs(
  x = M_multivariatenormal[,c("x","y","z")],
  col = M_multivariatenormal$color,
  pch = 19,
  main = "Scatterplot matrix of the multivariate normal density"
)

Multivariate normal random number generation.

Randomly generate at least 100 multivariate normal points.

Plot a 3-d scatterplot of your points.

M_rnvnorm <- mvtnorm::rmvnorm(
  n = 100,
  mean = v_mean,
  sigma = M_sigma
)
colnames(M_rnvnorm) <- c("x","y","z")
M_rnvnorm <- as.data.frame(
  x = M_rnvnorm
)
p <- plotly::plot_ly(
  x = M_rnvnorm$x,
  y = M_rnvnorm$y,
  z = M_rnvnorm$z,
  type="scatter3d",
  mode="markers"
)
p

Multivariate probabilities

Compute the probability that an observation is in this region:

mvtnorm::pmvnorm(
  lower = c(-1,-2,-2),
  upper = c(2,1,3),
  mean = v_mean,
  sigma =M_sigma
)
## [1] 0.17455
## attr(,"error")
## [1] 1.095076e-06
## attr(,"msg")
## [1] "Normal Completion"